/**
 * @fileoverview Giant Interective Group, Inc. Javascript Library v#version.
 * 该Javascript UI库是基于jQuery的扩展。
 * <pre>
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * </pre>
 *
 * @version #version, #date
 * @author  Zhangkai
 * Depend on jQuery 1.3.x
 */
if (typeof giant === 'undefined')
    var giant = {};

if (typeof giant.ui === 'undefined')
    giant.ui = {};
(function($) {
    giant.ui.emailauto = function(options, callBack) {
        this.opts = $.extend({}, giant.ui.emailauto.defaults, options);
        this._init();
        this._isShow = false;
        this._activeIndex = 0;
    };
    giant.ui.emailauto.prototype = {
        _init:function() {
            var $this = this;
            $(this.opts.input).bind("focus", function() {
                $(this).attr("class", "focus");
            });
            $(this.opts.input).bind("blur", function() {
                $(this).attr("class", "normal");
                if ($this._isShow) {
                    if( $($this.opts.input).val().length>0){
                     $($this.opts.input).val($($this.opts.result).find("li.hover").text());
                    }
                    $this.hideTip();
                }
            });
            $(this.opts.input).bind("keyup", function(event) {
                switch (event.keyCode) {
                    case 13:        //回车键
                        if ($this._isShow) {
                            $($this.opts.input).val($($this.opts.result).find("li.hover").text());
                             $this.hideTip();
                        }
                        break;
                    case 37:
                        break;
                    case 38:
                        if ($this._isShow) {
                            $this.activeIndexLower();
                            $($this.opts.result).find("li").removeClass("hover");
                            $($this.opts.result).find("li").eq($this._activeIndex).addClass("hover");
                        }
                        break;
                    case 39:
                        break;
                    case 40:     //down 键
                        if ($this._isShow) {
                            $this.activeIndexAdd();
                            $($this.opts.result).find("li").removeClass("hover");
                            $($this.opts.result).find("li").eq($this._activeIndex).addClass("hover");
                        }
                        break;
                    default:
                        if ($(this).val().indexOf('@') > -1) {
                            $this.hideTip();
                        } else {
                           $this.showTip();
                            $($this.opts.result).find("li span").text($(this).val());
                        }
                }
            });
            $($this.opts.result).find("li").each(function(index) {
                $(this).bind("mouseover", function() {
                    if ($this._isShow) {
                        $($this.opts.result).find("li").removeClass("hover");
                        $(this).addClass("hover");
                        $this._activeIndex = index;
                    }
                });
            });
            $($this.opts.result).find("li").click(function() {
                  if ($this._isShow) {
                            $($this.opts.input).val($($this.opts.result).find("li.hover").text());
                             $this.hideTip();
                        }
            });
        },
        showTip:function() {
            if (!this._isShow) {
                $(this.opts.result).show("fast");
                this.getPosition();
                this._isShow = true;
            }
        },
        hideTip:function() {
            if (this._isShow) {
                $(this.opts.result).hide("fast");
                this._isShow = false;
                this._activeIndex = 0;
            }
        },
        activeIndexAdd:function() {
            this._activeIndex++;
            if (this._activeIndex >= $(this.opts.result).find("li").size()) {
                this._activeIndex = 0;
            }
        },
        activeIndexLower:function() {
            this._activeIndex--;
            if (this._activeIndex < 0) {
                this._activeIndex = $(this.opts.result).find("li").size() - 1;
            }
        },
        getPosition:function(){
            $(this.opts.result).css({
                "left": $(this.opts.input).position().left+"px",
                "top":$(this.opts.input).position().top+22+"px"
            });
        }
    }
    /**
     * 默认参数<br / >
     * input==>输入框的xpath路径<br />
     * result==>提示列表的xpath路径<br />
     */
    giant.ui.emailauto.defaults = {
        input:"#txtUserName",
        result:"#ui-emailauto-tip"
    };
})(jQuery);